package com.ruoyi.framework.security.filter;

import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.context.SpringContext;
import com.ruoyi.common.core.resource.OperatorUserSystem;
import com.ruoyi.framework.utils.SessionHelper;
import com.ruoyi.system.domain.SysVersion;
import com.ruoyi.system.mapper.SysVersionMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Optional;

/**
 * @Author xiaom
 * @Date 2020/12/23 16:15
 * @Version 1.0.0
 * @Description <>
 **/
@Component
public class VersionFilter extends OncePerRequestFilter implements AbsFilter{

    protected final Logger logger = LoggerFactory.getLogger(VersionFilter.class);

    private final static String _VERSION = "version";

    @Autowired
    private SysVersionMapper sysVersionMapper;

    @Override
    protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException {

        Optional<String> clientType = SessionHelper.getRequestHeader(com.ruoyi.common.constant.Constants.OPERATOR_USER_SYSTEM_HEADER);

        if(!clientType.isPresent() || !clientType.get().equals(OperatorUserSystem.CLIENT_USER.name())){
            filterChain.doFilter(httpServletRequest, httpServletResponse);
            return;
        }
        if(httpServletRequest.getRequestURI().equals("/client-version/getVersion")){
            filterChain.doFilter(httpServletRequest, httpServletResponse);
            return;
        }
        Optional<String> versionOpt = SessionHelper.getRequestHeader(_VERSION);

        if(!versionOpt.isPresent()){
            responseException(httpServletResponse,new Exception("异常版本"), HttpStatus.VERSION_ERR);
            return;
        }

        //检查版本是否一致
        if(clientType.get().equals(OperatorUserSystem.CLIENT_USER.name())){
            SysVersion sysVersion = sysVersionMapper.selectLastVersion();
            if(sysVersion == null){
                filterChain.doFilter(httpServletRequest, httpServletResponse);
                return;
            }
            if(!sysVersion.getVersion().equals(versionOpt.get()) && sysVersion.getForce() == 1){
                logger.debug("异常的版本请求,ip:[{}] 最新版:[{}] 客户端版本:[{}]",SessionHelper.getContextRequest().getRemoteAddr(),sysVersion.getVersion(),versionOpt.get());
                responseException(httpServletResponse,new Exception("请升级最新版本"),HttpStatus.VERSION_ERR);
                return;
            }
        }
        filterChain.doFilter(httpServletRequest, httpServletResponse);
    }

    @Override
    public Logger getLogger() {
        return this.logger;
    }
}
